import string
from urllib.parse import urljoin

import requests
import random
import os
import re

# 爱奇艺排行榜页面URL
url = 'https://www.iqiyi.com/ranks1PCW/-1/0'

# 请求参数
datas = {
    'rid': '181',
    'type': 'all',
    'web_location': '333.934',
    'w_rid': 'd9c2e6b911c1757fae2d2a2c78971e53',
    'wts': '1728559155'
}

# 基础User-Agent字符串
base_user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'


# 随机选择一个字母并插入到 User-Agent 中的随机位置
def generate_random_user_agent(base_user_agent):
    random_letter = random.choice(string.ascii_letters)
    position = random.randint(0, len(base_user_agent))
    new_user_agent = base_user_agent[:position] + random_letter + base_user_agent[position:]
    return new_user_agent


# 设置请求头部，包含随机生成的 User-Agent
headers = {
    'User-Agent': generate_random_user_agent(base_user_agent)
}

# 发送HTTP请求，获取网页内容
response = requests.get(url, params=datas, headers=headers)


# 从网页内容中提取数据
def extract_data(response_text):
    # 定义正则表达式模式，用于匹配标题、标签、描述和详情
    title_pattern = r'<div title="([^"]+)" class="rvi__tit1"'
    tags_pattern = r'<span class="rvi__tag rvi__tag1".*?>(.*?)</span>'
    description_pattern = r'<p title="([^"]+)" class="rvi__des2"'
    details_pattern = r'<div title="([^"]+)" class="rvi__type1"'

    # 使用正则表达式提取数据
    titles = re.findall(title_pattern, response_text)
    tags = re.findall(tags_pattern, response_text)
    descriptions = re.findall(description_pattern, response_text)
    details = re.findall(details_pattern, response_text)

    # 将提取的数据整理成字典列表
    results = []
    for i, title in enumerate(titles):
        result = {
            "title": title,
            "tags": tags[i * 2:(i + 1) * 2],
            "description": descriptions[i] if i < len(descriptions) else "",
            "details": details[i] if i < len(details) else ""
        }
        results.append(result)

    return results


# 从网页内容中提取数据并下载图片
def extract_data_and_download_images(response_text, image_folder="picture"):
    # 定义正则表达式模式，用于匹配标题和图片URL
    title_pattern = r'<div title="([^"]+)" class="rvi__tit1"'
    image_pattern = r'<picture id="([^"]+)"'

    # 使用正则表达式提取数据
    titles = re.findall(title_pattern, response_text)
    image_urls = re.findall(image_pattern, response_text)

    # 创建图片保存文件夹
    if not os.path.exists(image_folder):
        os.makedirs(image_folder)

    # 下载图片
    for i, image_url in enumerate(image_urls):
        try:
            full_image_url = f"https:{image_url}"
            response = requests.get(full_image_url, stream=True)
            if response.status_code == 200:
                valid_title = re.sub(r'[\\/:"*?<>|]', "_", titles[i])
                image_path = os.path.join(image_folder, f"{valid_title}.jpg")
                with open(image_path, "wb") as f:
                    for chunk in response.iter_content(1024):
                        f.write(chunk)
                print(f"已保存海报: {image_path}")
            else:
                print(f"无法下载海报: {full_image_url}，HTTP状态码: {response.status_code}")
        except Exception as e:
            print(f"下载图片出错: {full_image_url}，错误: {e}")


# 解析网页内容并提取数据
response_text = response.text
data = extract_data(response_text)

# 打印提取的数据
for item in data:
    print(f"作品名: {item['title']}")
    print(f"详情: {item['details']}")
    print(f"描述: {item['description']}")
    print("-" * 40)


# 将数据保存到文件
def save_to_file(data, filename="result.txt"):
    with open(filename, "w", encoding="utf-8") as f:
        for item in data:
            f.write(f"作品名: {item['title']}\n")
            f.write(f"详情: {item['details']}\n")
            f.write(f"描述: {item['description']}\n")
            f.write("-" * 40 + "\n")
    print(f"数据已保存到 {filename}")


# 保存数据到文件并下载图片
save_to_file(data)
extract_data_and_download_images(response_text)
